import { useTrafficStore } from "@/stores/counter";
import { format } from 'date-fns'
import { setPopup } from "@/ol-api/popup"
const trafficStore = useTrafficStore();

export class Query {
    /*点查询*/
     /**
     *@param {Array} position = [114.30,30.50]
     *@param {Object} service = {name:"xm",layerId:2}
     * @param {Object} $map =    $map地图实例
     *
     * */
     static queryByPnt({
        position,
        service,
        $map
    }) {
        let pointObj = new Zondy.Object.Point2D(position[0], position[1]);
        //设置查询点的搜索半径
        /* 根据查询区域的大小灵活调整的 */
        pointObj.nearDis = 0.0008; //单位：度。0.001表示111米
        /* 1、Feature设置查询结构 */
        //初始化查询结构对象，设置查询结构包含几何信息
        var queryStruct = new Zondy.Service.QueryFeatureStruct();
        //是否包含几何图形信息
        queryStruct.IncludeGeometry = true;
        //是否包含属性信息
        queryStruct.IncludeAttribute = true;
        //是否包含图形显示参数
        queryStruct.IncludeWebGraphic = false;
        //2、指定查询规则
        var rule = new Zondy.Service.QueryFeatureRule({
            //是否将要素的可见性计算在内
            EnableDisplayCondition: false,
            //是否完全包含
            MustInside: false,
            //是否仅比较要素的外包矩形
            CompareRectOnly: false,
            //是否相交
            Intersect: true
        });
        //3、实例化查询参数对象
        var queryParam = new Zondy.Service.QueryParameter({
            geometry: pointObj,
            resultFormat: "json",
            struct: queryStruct,
            rule: rule
        });
        //设置查询分页号
        queryParam.pageIndex = 0;
        //设置查询要素数目
        queryParam.recordNumber = 1;

        //4、实例化地图文档查询服务对象
        var queryService = new Zondy.Service.QueryDocFeature(queryParam, service.name, service.layerId, {

        });
        //执行查询操作，querySuccess为查询回调函数
        queryService.query(this.queryPntSuccess.bind(null, $map));
    }
    static queryByPnt_bak({
        position,
        service,
        success
    }) {
        let pointObj = new Zondy.Object.Point2D(position[0], position[1]);
        //设置查询点的搜索半径
        /* 根据查询区域的大小灵活调整的 */
        pointObj.nearDis = 0.5;
        /* 1、Feature设置查询结构 */
        //初始化查询结构对象，设置查询结构包含几何信息
        var queryStruct = new Zondy.Service.QueryFeatureStruct();
        //是否包含几何图形信息
        queryStruct.IncludeGeometry = true;
        //是否包含属性信息
        queryStruct.IncludeAttribute = true;
        //是否包含图形显示参数
        queryStruct.IncludeWebGraphic = false;
        //2、指定查询规则
        var rule = new Zondy.Service.QueryFeatureRule({
            //是否将要素的可见性计算在内
            EnableDisplayCondition: false,
            //是否完全包含
            MustInside: false,
            //是否仅比较要素的外包矩形
            CompareRectOnly: false,
            //是否相交
            Intersect: true
        });
        //3、实例化查询参数对象
        var queryParam = new Zondy.Service.QueryParameter({
            geometry: pointObj,
            resultFormat: "json",
            struct: queryStruct,
            rule: rule
        });

        //4、实例化地图文档查询服务对象
        var queryService = new Zondy.Service.QueryDocFeature(queryParam, service.name, service.layerId, {

        });
        //执行查询操作，querySuccess为查询回调函数
        queryService.query(this.querySuccess(success));
    }
     /*面查询*/
     /**
     *@param {Array} geometry = new ol.geom.Polygon([[114.30,30.50],[114.35,30.50],[114.35,30.55],[114.30,30.55],[114.30,30.50]])
     *@param {Object} service = {name:"xm",layerId:2} 
     * @param {Object} success =    查询成功回调函数
     *  
     * */
    /* geometry是ol要素的geometry对象 */
    static queryByGeometry({
        geometry,
        service,
        success
    }) {
        var queryStruct = new Zondy.Service.QueryFeatureStruct();
        //是否包含几何图形信息
        queryStruct.IncludeGeometry = true;
        //是否包含属性信息
        queryStruct.IncludeAttribute = true;
        //是否包含图形显示参数
        queryStruct.IncludeWebGraphic = false;
        //创建一个用于查询的区
        var geomObj = new Zondy.Object.Polygon();
        geomObj.setByOL(geometry);
        //指定查询规则
        var rule = new Zondy.Service.QueryFeatureRule({
            //是否将要素的可见性计算在内
            EnableDisplayCondition: false,
            //是否完全包含
            MustInside: false,
            //是否仅比较要素的外包矩形
            CompareRectOnly: false,
            //是否相交
            Intersect: true
        });
        //实例化查询参数对象
        var queryParam = new Zondy.Service.QueryParameter({
            geometry: geomObj,
            resultFormat: "json",
            struct: queryStruct,
            rule: rule
        });
        //设置查询分页号
        queryParam.pageIndex = 0;
        //设置查询要素数目
        /* limit */
        queryParam.recordNumber = 100;
        //实例化地图文档查询服务对象
        var queryService = new Zondy.Service.QueryDocFeature(queryParam, service.name, service.layerId, {

        });
        //执行查询操作，querySuccess为查询回调函数
        queryService.query(this.querySuccess(success));
    }
    static queryByLayerId({
        service,
        success
    }) {
        var queryStruct = new Zondy.Service.QueryFeatureStruct();
        //是否包含几何图形信息
        queryStruct.IncludeGeometry = true;
        //是否包含属性信息
        queryStruct.IncludeAttribute = true;
        //是否包含图形显示参数
        queryStruct.IncludeWebGraphic = false;
        //2、指定查询规则
        var rule = new Zondy.Service.QueryFeatureRule({
            //是否将要素的可见性计算在内
            EnableDisplayCondition: false,
            //是否完全包含
            MustInside: false,
            //是否仅比较要素的外包矩形
            CompareRectOnly: false,
            //是否相交
            Intersect: true
        });
        //实例化查询参数对象
        var queryParam = new Zondy.Service.QueryParameter({
            resultFormat: "json",
            struct: queryStruct,
            rule: rule
        });
        //设置查询分页号
        queryParam.pageIndex = 0;
        //设置查询要素数目
        queryParam.recordNumber = 100;
        //实例化地图文档查询服务对象
        var queryService = new Zondy.Service.QueryDocFeature(queryParam, service.name, service.layerId, {

        });
        //执行查询操作，querySuccess为查询回调函数
        queryService.query(this.querySuccess(success));
    }
    /* where = {key:"name",value:"汉"} */
    static queryByAttr({
        where,
        service,
        success
    }) {
        //1、初始化查询结构对象，设置查询结构包含几何信息
        var queryStruct = new Zondy.Service.QueryFeatureStruct();
        //是否包含几何图形信息
        queryStruct.IncludeGeometry = true;
        //是否包含属性信息
        queryStruct.IncludeAttribute = true;
        //是否包含图形显示参数
        queryStruct.IncludeWebGraphic = false;
        //2、实例化查询参数对象
        var queryParam = new Zondy.Service.QueryParameter({
            resultFormat: "json",
            struct: queryStruct
        });
        /* mysql */
        // queryParam.where = `name = '${name}'`;
        /* includes */
        queryParam.where = `${where.key} like '%${where.value}%'`;
        var queryService = new Zondy.Service.QueryDocFeature(queryParam, service.name, service.layerId, {

        });
        //执行查询操作，querySuccess为查询回调函数
        queryService.query(this.querySuccess(success));
    }
    /* fid=Number|Arrray */
    static queryByFid({
        fid,
        service,
        success
    }) {
        //初始化查询结构对象，设置查询结构包含几何信息
        var queryStruct = new Zondy.Service.QueryFeatureStruct();
        //是否包含几何图形信息
        queryStruct.IncludeGeometry = true;
        //是否包含属性信息
        queryStruct.IncludeAttribute = true;
        //是否包含图形显示参数
        queryStruct.IncludeWebGraphic = false;
        //实例化查询参数对象
        var queryParam = new Zondy.Service.QueryParameter({
            objectIds: fid,
            resultFormat: "json",
            struct: queryStruct
        });
        var queryService = new Zondy.Service.QueryDocFeature(queryParam, service.name, service.layerId, {

        });
        queryService.query(this.querySuccess(success));
    }
    static querySuccess(success) {
        return function (data) {
            /* data mapgis数据 */
            var format = new Zondy.Format.PolygonJSON();
            //三、将MapGIS要素JSON反序列化为ol.Feature类型数组
            var features = format.read(data);
            success(features);
        }
    }
    static queryPntSuccess(map, result) {
        console.log(result);
        if (result.SFEleArray.length==0) {
          ElMessage.error('没有查找到要素！')
        }
        else {
          //获取选中点要素FID
          trafficStore.fid = result.SFEleArray[0].FID;
          //获取选中点要素几何信息
          trafficStore.eventPosition = [result.SFEleArray[0].fGeom.PntGeom[0].Dot.x, result.SFEleArray[0].fGeom.PntGeom[0].Dot.y];
          console.log(result);
          //显示弹窗
          setPopup(trafficStore.eventPosition, map);
          //获取选中点要素属性值
          trafficStore.updateEventList = result.SFEleArray[0].AttValue.slice(0, -1);
          // 处理时间数据格式
          trafficStore.timeLabel = format(result.SFEleArray[0].AttValue[3], 'yyyy-mm-dd HH:mm:ss');
        }
      }
}